{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Export the mean of the SD maps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import the libraries\n",
    "import ee\n",
    "import pandas as pd\n",
    "import os\n",
    "import numpy as np\n",
    "import random\n",
    "from random import sample\n",
    "import itertools \n",
    "import geopandas as gpd\n",
    "from sklearn.metrics import r2_score\n",
    "from termcolor import colored # this is allocate colour and fonts type for the print title and text\n",
    "from IPython.display import display, HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# initialize the earth engine API\n",
    "ee.Initialize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the basic maps that needed for the analysis\n",
    "# load the carbon concentration map\n",
    "carbonConcentration = ee.Image(\"users/leonidmoore/ForestBiomass/Biome_level_Wood_Carbon_Conentration_Map\")\n",
    "# load the root shoot ratio map\n",
    "rootShootRatio = ee.Image(\"users/leonidmoore/ForestBiomass/Root_shoot_ratio_Map\").unmask()\n",
    "# load the two composites tha will be used in the analysis\n",
    "compositeImage =ee.Image(\"users/leonidmoore/ForestBiomass/20200915_Forest_Biomass_Predictors_Image\")\n",
    "compositeImageNew = ee.Image(\"projects/crowtherlab/Composite/CrowtherLab_Composite_30ArcSec\")\n",
    "# load the present forest cover\n",
    "presentForestCover = compositeImage.select('PresentTreeCover')\n",
    "potentialForestCover = ee.Image(\"users/leonidmoore/ForestBiomass/Bastin_et_al_2019_Potential_Forest_Cover_Adjusted\").unmask().rename('PotentialForestCover')\n",
    "potentialMask = potentialForestCover.gt(0)\n",
    "# load the biome layer \n",
    "biomeLayer = compositeImage.select(\"WWF_Biome\")\n",
    "# define a pixel area layer with unit km2\n",
    "pixelAreaMap = ee.Image.pixelArea().divide(10000);\n",
    "# define the boundary geography reference\n",
    "unboundedGeo = ee.Geometry.Polygon([-180, 88, 0, 88, 180, 88, 180, -88, 0, -88, -180, -88], None, False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## load all the GS and SD maps of present biomass density"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the GS carbon density maps of present biomass\n",
    "carbonDensityGS_Max = ee.Image(\"users/leonidmoore/ForestBiomass/GroundSourcedModel/EnsambledMaps/Predicted_GS1_MaxScaler_Present_density_Ensambled_Mean_20230427\").unmask()\n",
    "carbonDensityGS_Mean = ee.Image(\"users/leonidmoore/ForestBiomass/GroundSourcedModel/EnsambledMaps/Predicted_GS1_MeanScaler_Present_density_Ensambled_Mean_20230427\").unmask()\n",
    "# load the SD carbon density maps of present biomass\n",
    "carbonDensityWK =  ee.Image(\"users/leonidmoore/ForestBiomass/WalkerMap/reprojected_Walker_map_1km\").unmask()\n",
    "# since the SD map from Arnan is biomass density, therefore, we directly transfer thre biomass density into carbon density by multiplying the carbon concentration\n",
    "carbonDensitySD =  ee.Image(\"users/leonidmoore/ForestBiomass/RemoteSensingModel/ESA_CCI_AGB_Map_bias_corrected_1km_2010\").multiply(carbonConcentration).unmask()\n",
    "carbonDensityHM =  ee.Image(\"users/leonidmoore/ForestBiomass/SpawnMap/Spawn_Harmonized_AGB_density_Map_1km\").select('agb').unmask()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# compose the  images into a multiband image\n",
    "carbonDensityImage = carbonDensityGS_Max.addBands(carbonDensityGS_Max).addBands(carbonDensityGS_Mean).addBands(carbonDensityWK).addBands(carbonDensitySD).addBands(carbonDensityHM).unmask()\n",
    "# apply the reducer \n",
    "carbonDensityPresent = carbonDensityImage.reduce(ee.Reducer.mean()).mask(presentForestCover.gt(0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## load all the GS and SD maps of potential biomass density"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the potential carbon density for the GS models\n",
    "potentialDensity_Max1 = ee.Image(\"users/leonidmoore/ForestBiomass/GroundSourcedModel/EnsambledMaps/Predicted_GS1_MaxScaler_Potential_density_Ensambled_Mean_20230427\").unmask()\n",
    "potentialDensity_Max2 = ee.Image(\"users/leonidmoore/ForestBiomass/GroundSourcedModel/EnsambledMaps/Predicted_GS2_MaxScaler_Potential_density_Ensambled_Mean_20230427\").unmask()\n",
    "\n",
    "potentialDensity_Mean1 = ee.Image(\"users/leonidmoore/ForestBiomass/GroundSourcedModel/EnsambledMaps/Predicted_GS1_MeanScaler_Potential_density_Ensambled_Mean_20230427\").unmask()\n",
    "potentialDensity_Mean2 = ee.Image(\"users/leonidmoore/ForestBiomass/GroundSourcedModel/EnsambledMaps/Predicted_GS2_MeanScaler_Potential_density_Ensambled_Mean_20230427\").unmask()\n",
    "\n",
    "# load the potential carbon density maps for SD models\n",
    "potentialDensityWK1 = ee.Image(\"users/nordmannmoore/ForestBiomass/RemoteSensingModel/EnsambleMaps/Predicted_WK1_Potential_density_Ensambled_Mean\").unmask()\n",
    "potentialDensityWK2 = ee.Image(\"users/nordmannmoore/ForestBiomass/RemoteSensingModel/EnsambleMaps/Predicted_WK2_Potential_density_Ensambled_Mean\").unmask()\n",
    "\n",
    "potentialDensitySD1 = ee.Image(\"users/nordmannmoore/ForestBiomass/RemoteSensingModel/EnsambleMaps/Predicted_SD1_Potential_density_Ensambled_Mean\").unmask()\n",
    "potentialDensitySD2 = ee.Image(\"users/nordmannmoore/ForestBiomass/RemoteSensingModel/EnsambleMaps/Predicted_SD2_Potential_density_Ensambled_Mean\").unmask()\n",
    "\n",
    "potentialDensityHM1 = ee.Image(\"users/leonidmoore/ForestBiomass/GroundSourcedModel/EnsambledMaps/Predicted_HM1_Potential_density_Ensambled_Mean\").unmask()\n",
    "potentialDensityHM2 = ee.Image(\"users/nordmannmoore/ForestBiomass/RemoteSensingModel/EnsambleMaps/Predicted_HM2_Potential_density_Ensambled_Mean\").unmask()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adjust the map for each model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# define a functiion to do the values adjustment\n",
    "def adjFunc(presentDen,potentialDen):\n",
    "    # check the difference of the two density maps\n",
    "    potentialHigher = potentialDen.subtract(presentDen).gte(0)\n",
    "    potentialLower = potentialDen.subtract(presentDen).lt(0)\n",
    "    # replace the lower potential value by present biomass density value\n",
    "    adjPotentialDen = presentDen.multiply(potentialLower).add(potentialDen.multiply(potentialHigher))\n",
    "    return(adjPotentialDen)\n",
    "\n",
    "# GS max\n",
    "tgbPotentialDensityGS_Max1 = adjFunc(presentDen = carbonDensityGS_Max,potentialDen = potentialDensity_Max1).multiply(rootShootRatio.add(1))\n",
    "tgbPotentialDensityGS_Max2 = adjFunc(presentDen = carbonDensityGS_Max,potentialDen = potentialDensity_Max2).multiply(rootShootRatio.add(1))\n",
    "tgbPresentDensityGS_Max = carbonDensityGS_Max.multiply(rootShootRatio.add(1))\n",
    "# GS Mean \n",
    "tgbPotentialDensityGS_Mean1 = adjFunc(presentDen = carbonDensityGS_Mean,potentialDen = potentialDensity_Mean1).multiply(rootShootRatio.add(1))\n",
    "tgbPotentialDensityGS_Mean2 = adjFunc(presentDen = carbonDensityGS_Mean,potentialDen = potentialDensity_Mean2).multiply(rootShootRatio.add(1))\n",
    "tgbPresentDensityGS_Mean = carbonDensityGS_Mean.multiply(rootShootRatio.add(1))\n",
    "# SD\n",
    "tgbPotentialDensitySD1 = adjFunc(presentDen = carbonDensitySD,potentialDen = potentialDensitySD1).multiply(rootShootRatio.add(1))\n",
    "tgbPotentialDensitySD2 = adjFunc(presentDen = carbonDensitySD,potentialDen = potentialDensitySD2).multiply(rootShootRatio.add(1))\n",
    "tgbPresentDensitySD = carbonDensitySD.multiply(rootShootRatio.add(1))\n",
    "# HM\n",
    "tgbPotentialDensityHM1 = adjFunc(presentDen = carbonDensityHM,potentialDen = potentialDensityHM1).multiply(rootShootRatio.add(1))\n",
    "tgbPotentialDensityHM2 = adjFunc(presentDen = carbonDensityHM,potentialDen = potentialDensityHM2).multiply(rootShootRatio.add(1))\n",
    "tgbPresentDensityHM = carbonDensityHM.multiply(rootShootRatio.add(1))\n",
    "\n",
    "# WK\n",
    "tgbPotentialDensityWK1 = adjFunc(presentDen = carbonDensityWK,potentialDen = potentialDensityWK1).multiply(rootShootRatio.add(1))\n",
    "tgbPotentialDensityWK2 = adjFunc(presentDen = carbonDensityWK,potentialDen = potentialDensityWK2).multiply(rootShootRatio.add(1))\n",
    "tgbPresentDensityWK = carbonDensityWK.multiply(rootShootRatio.add(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Export the full and net TGB potential maps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'state': 'READY',\n",
       " 'description': 'Ensembled_net_potential_map_Export',\n",
       " 'creation_timestamp_ms': 1683295038627,\n",
       " 'update_timestamp_ms': 1683295038627,\n",
       " 'start_timestamp_ms': 0,\n",
       " 'task_type': 'EXPORT_IMAGE',\n",
       " 'id': 'WYPQEYTWTYR64SQQQVEE6WTL',\n",
       " 'name': 'projects/earthengine-legacy/operations/WYPQEYTWTYR64SQQQVEE6WTL'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tgbPresentDensityImage = tgbPresentDensityGS_Max.addBands(tgbPresentDensityGS_Mean).addBands(tgbPresentDensitySD).addBands(tgbPresentDensityHM).addBands(tgbPresentDensityWK)\n",
    "# apply the reducer \n",
    "tgbPresentDensity = tgbPresentDensityImage.reduce(ee.Reducer.mean())\n",
    "\n",
    "# compose the  images into a multiband image\n",
    "tgbPotentialDensityImage = tgbPotentialDensityGS_Max1.addBands(tgbPotentialDensityGS_Max2).addBands(tgbPotentialDensityGS_Mean1).addBands(tgbPotentialDensityGS_Mean2).addBands(tgbPotentialDensitySD1).addBands(tgbPotentialDensitySD2).addBands(tgbPotentialDensityHM1).addBands(tgbPotentialDensityHM2).addBands(tgbPotentialDensityWK1).addBands(tgbPotentialDensityWK2)\n",
    "# apply the reducer \n",
    "tgbPotentialDensity = tgbPotentialDensityImage.reduce(ee.Reducer.mean())\n",
    "tgbPotentialDensityNet = tgbPotentialDensity.subtract(tgbPresentDensity)\n",
    "\n",
    "exportTask = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensity.mask(potentialMask),\n",
    "                                                  description = 'Ensembled_full_potential_map_Export',\n",
    "                                                  fileNamePrefix = 'ForestBiomassExport/Full_TGB_potential_Map_of_ensembled_mean',\n",
    "                                                  region = unboundedGeo,\n",
    "                                                  bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                  crs = 'EPSG:4326',\n",
    "                                                  crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                  maxPixels = 1e13,\n",
    "                                                  fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTask.start()\n",
    "# show the task status\n",
    "exportTask.status()\n",
    "exportTaskDiff = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityNet.mask(potentialMask),\n",
    "                                                      description = 'Ensembled_net_potential_map_Export',\n",
    "                                                      fileNamePrefix = 'ForestBiomassExport/Net_TGB_potential_Map_of_ensembled_mean',\n",
    "                                                      region = unboundedGeo,\n",
    "                                                      bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                      crs = 'EPSG:4326',\n",
    "                                                      crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                      maxPixels = 1e13,\n",
    "                                                      fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskDiff.start()\n",
    "# show the task status\n",
    "exportTaskDiff.status()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Export the full and net TGB potential maps for each model type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'state': 'READY',\n",
       " 'description': 'GS_Max2_net_potential_map_Export',\n",
       " 'creation_timestamp_ms': 1683295052003,\n",
       " 'update_timestamp_ms': 1683295052003,\n",
       " 'start_timestamp_ms': 0,\n",
       " 'task_type': 'EXPORT_IMAGE',\n",
       " 'id': 'TLVPYFQ2JNQYEONUHPMGUC6P',\n",
       " 'name': 'projects/earthengine-legacy/operations/TLVPYFQ2JNQYEONUHPMGUC6P'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exportTaskGSMax1 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityGS_Max1.mask(potentialMask),\n",
    "                                                  description = 'GS_Max1_full_potential_map_Export',\n",
    "                                                  fileNamePrefix = 'ForestBiomassExport/GS_Max1_Full_TGB_potential_Map',\n",
    "                                                  region = unboundedGeo,\n",
    "                                                  bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                  crs = 'EPSG:4326',\n",
    "                                                  crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                  maxPixels = 1e13,\n",
    "                                                  fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskGSMax1.start()\n",
    "# show the task status\n",
    "exportTaskGSMax1.status()\n",
    "\n",
    "exportTaskGSMax2 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityGS_Max2.mask(potentialMask),\n",
    "                                                  description = 'GS_Max2_full_potential_map_Export',\n",
    "                                                  fileNamePrefix = 'ForestBiomassExport/GS_Max2_Full_TGB_potential_Map',\n",
    "                                                  region = unboundedGeo,\n",
    "                                                  bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                  crs = 'EPSG:4326',\n",
    "                                                  crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                  maxPixels = 1e13,\n",
    "                                                  fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskGSMax2.start()\n",
    "# show the task status\n",
    "exportTaskGSMax2.status()\n",
    "\n",
    "\n",
    "\n",
    "exportTaskGSMaxDiff1 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityGS_Max1.subtract(tgbPresentDensityGS_Max).mask(potentialMask),\n",
    "                                                      description = 'GS_Max1_net_potential_map_Export',\n",
    "                                                      fileNamePrefix = 'ForestBiomassExport/GS_Max1_Net_TGB_potential_Map',\n",
    "                                                      region = unboundedGeo,\n",
    "                                                      bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                      crs = 'EPSG:4326',\n",
    "                                                      crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                      maxPixels = 1e13,\n",
    "                                                      fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskGSMaxDiff1.start()\n",
    "# show the task status\n",
    "exportTaskGSMaxDiff1.status()\n",
    "\n",
    "\n",
    "exportTaskGSMaxDiff2 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityGS_Max2.subtract(tgbPresentDensityGS_Max).mask(potentialMask),\n",
    "                                                      description = 'GS_Max2_net_potential_map_Export',\n",
    "                                                      fileNamePrefix = 'ForestBiomassExport/GS_Max2_Net_TGB_potential_Map',\n",
    "                                                      region = unboundedGeo,\n",
    "                                                      bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                      crs = 'EPSG:4326',\n",
    "                                                      crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                      maxPixels = 1e13,\n",
    "                                                      fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskGSMaxDiff2.start()\n",
    "# show the task status\n",
    "exportTaskGSMaxDiff2.status()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'state': 'READY',\n",
       " 'description': 'GS_Mean2_net_potential_map_Export',\n",
       " 'creation_timestamp_ms': 1683295063892,\n",
       " 'update_timestamp_ms': 1683295063892,\n",
       " 'start_timestamp_ms': 0,\n",
       " 'task_type': 'EXPORT_IMAGE',\n",
       " 'id': 'USWLLDZZC24KVRCXGUB54SIH',\n",
       " 'name': 'projects/earthengine-legacy/operations/USWLLDZZC24KVRCXGUB54SIH'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exportTaskGSMean1 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityGS_Mean1.mask(potentialMask),\n",
    "                                                  description = 'GS_Mean1_full_potential_map_Export',\n",
    "                                                  fileNamePrefix = 'ForestBiomassExport/GS_Mean1_Full_TGB_potential_Map',\n",
    "                                                  region = unboundedGeo,\n",
    "                                                  bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                  crs = 'EPSG:4326',\n",
    "                                                  crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                  maxPixels = 1e13,\n",
    "                                                  fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskGSMean1.start()\n",
    "# show the task status\n",
    "exportTaskGSMean1.status()\n",
    "\n",
    "exportTaskGSMean2 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityGS_Mean2.mask(potentialMask),\n",
    "                                                  description = 'GS_Mean2_full_potential_map_Export',\n",
    "                                                  fileNamePrefix = 'ForestBiomassExport/GS_Mean2_Full_TGB_potential_Map',\n",
    "                                                  region = unboundedGeo,\n",
    "                                                  bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                  crs = 'EPSG:4326',\n",
    "                                                  crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                  maxPixels = 1e13,\n",
    "                                                  fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskGSMean2.start()\n",
    "# show the task status\n",
    "exportTaskGSMean2.status()\n",
    "\n",
    "\n",
    "\n",
    "exportTaskGSMeanDiff1 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityGS_Mean1.subtract(tgbPresentDensityGS_Mean).mask(potentialMask),\n",
    "                                                      description = 'GS_Mean1_net_potential_map_Export',\n",
    "                                                      fileNamePrefix = 'ForestBiomassExport/GS_Mean1_Net_TGB_potential_Map',\n",
    "                                                      region = unboundedGeo,\n",
    "                                                      bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                      crs = 'EPSG:4326',\n",
    "                                                      crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                      maxPixels = 1e13,\n",
    "                                                      fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskGSMeanDiff1.start()\n",
    "# show the task status\n",
    "exportTaskGSMeanDiff1.status()\n",
    "\n",
    "\n",
    "exportTaskGSMeanDiff2 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityGS_Mean2.subtract(tgbPresentDensityGS_Mean).mask(potentialMask),\n",
    "                                                      description = 'GS_Mean2_net_potential_map_Export',\n",
    "                                                      fileNamePrefix = 'ForestBiomassExport/GS_Mean2_Net_TGB_potential_Map',\n",
    "                                                      region = unboundedGeo,\n",
    "                                                      bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                      crs = 'EPSG:4326',\n",
    "                                                      crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                      maxPixels = 1e13,\n",
    "                                                      fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskGSMeanDiff2.start()\n",
    "# show the task status\n",
    "exportTaskGSMeanDiff2.status()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'state': 'READY',\n",
       " 'description': 'WK2_net_potential_map_Export',\n",
       " 'creation_timestamp_ms': 1683295066686,\n",
       " 'update_timestamp_ms': 1683295066686,\n",
       " 'start_timestamp_ms': 0,\n",
       " 'task_type': 'EXPORT_IMAGE',\n",
       " 'id': 'LJ5WZZK6NTXQAOHWKGXIKKWD',\n",
       " 'name': 'projects/earthengine-legacy/operations/LJ5WZZK6NTXQAOHWKGXIKKWD'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exportTaskWK1 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityWK1.mask(potentialMask),\n",
    "                                                  description = 'WK1_full_potential_map_Export',\n",
    "                                                  fileNamePrefix = 'ForestBiomassExport/WK1_Full_TGB_potential_Map',\n",
    "                                                  region = unboundedGeo,\n",
    "                                                  bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                  crs = 'EPSG:4326',\n",
    "                                                  crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                  maxPixels = 1e13,\n",
    "                                                  fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskWK1.start()\n",
    "# show the task status\n",
    "exportTaskWK1.status()\n",
    "\n",
    "exportTaskWK2 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityWK2.mask(potentialMask),\n",
    "                                                  description = 'WK2_full_potential_map_Export',\n",
    "                                                  fileNamePrefix = 'ForestBiomassExport/WK2_Full_TGB_potential_Map',\n",
    "                                                  region = unboundedGeo,\n",
    "                                                  bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                  crs = 'EPSG:4326',\n",
    "                                                  crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                  maxPixels = 1e13,\n",
    "                                                  fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskWK2.start()\n",
    "# show the task status\n",
    "exportTaskWK2.status()\n",
    "\n",
    "\n",
    "\n",
    "exportTaskWKDiff1 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityWK1.subtract(tgbPresentDensityWK).mask(potentialMask),\n",
    "                                                      description = 'WK1_net_potential_map_Export',\n",
    "                                                      fileNamePrefix = 'ForestBiomassExport/WK1_Net_TGB_potential_Map',\n",
    "                                                      region = unboundedGeo,\n",
    "                                                      bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                      crs = 'EPSG:4326',\n",
    "                                                      crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                      maxPixels = 1e13,\n",
    "                                                      fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskWKDiff1.start()\n",
    "# show the task status\n",
    "exportTaskWKDiff1.status()\n",
    "\n",
    "\n",
    "exportTaskWKDiff2 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityWK2.subtract(tgbPresentDensityWK).mask(potentialMask),\n",
    "                                                      description = 'WK2_net_potential_map_Export',\n",
    "                                                      fileNamePrefix = 'ForestBiomassExport/WK2_Net_TGB_potential_Map',\n",
    "                                                      region = unboundedGeo,\n",
    "                                                      bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                      crs = 'EPSG:4326',\n",
    "                                                      crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                      maxPixels = 1e13,\n",
    "                                                      fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskWKDiff2.start()\n",
    "# show the task status\n",
    "exportTaskWKDiff2.status()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'state': 'READY',\n",
       " 'description': 'HM2_net_potential_map_Export',\n",
       " 'creation_timestamp_ms': 1675161127047,\n",
       " 'update_timestamp_ms': 1675161127047,\n",
       " 'start_timestamp_ms': 0,\n",
       " 'task_type': 'EXPORT_IMAGE',\n",
       " 'id': 'U5NU4Y7VHI5B6G4R7IFMRYYO',\n",
       " 'name': 'projects/earthengine-legacy/operations/U5NU4Y7VHI5B6G4R7IFMRYYO'}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exportTaskHM1 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityHM1.mask(potentialMask),\n",
    "                                                  description = 'HM1_full_potential_map_Export',\n",
    "                                                  fileNamePrefix = 'ForestBiomassExport/HM1_Full_TGB_potential_Map',\n",
    "                                                  region = unboundedGeo,\n",
    "                                                  bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                  crs = 'EPSG:4326',\n",
    "                                                  crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                  maxPixels = 1e13,\n",
    "                                                  fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskHM1.start()\n",
    "# show the task status\n",
    "exportTaskHM1.status()\n",
    "\n",
    "exportTaskHM2 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityHM2.mask(potentialMask),\n",
    "                                                  description = 'HM2_full_potential_map_Export',\n",
    "                                                  fileNamePrefix = 'ForestBiomassExport/HM2_Full_TGB_potential_Map',\n",
    "                                                  region = unboundedGeo,\n",
    "                                                  bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                  crs = 'EPSG:4326',\n",
    "                                                  crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                  maxPixels = 1e13,\n",
    "                                                  fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskHM2.start()\n",
    "# show the task status\n",
    "exportTaskHM2.status()\n",
    "\n",
    "\n",
    "\n",
    "exportTaskHMDiff1 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityHM1.subtract(tgbPresentDensityHM).mask(potentialMask),\n",
    "                                                      description = 'HM1_net_potential_map_Export',\n",
    "                                                      fileNamePrefix = 'ForestBiomassExport/HM1_Net_TGB_potential_Map',\n",
    "                                                      region = unboundedGeo,\n",
    "                                                      bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                      crs = 'EPSG:4326',\n",
    "                                                      crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                      maxPixels = 1e13,\n",
    "                                                      fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskHMDiff1.start()\n",
    "# show the task status\n",
    "exportTaskHMDiff1.status()\n",
    "\n",
    "\n",
    "exportTaskHMDiff2 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensityHM2.subtract(tgbPresentDensityHM).mask(potentialMask),\n",
    "                                                      description = 'HM2_net_potential_map_Export',\n",
    "                                                      fileNamePrefix = 'ForestBiomassExport/HM2_Net_TGB_potential_Map',\n",
    "                                                      region = unboundedGeo,\n",
    "                                                      bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                      crs = 'EPSG:4326',\n",
    "                                                      crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                      maxPixels = 1e13,\n",
    "                                                      fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskHMDiff2.start()\n",
    "# show the task status\n",
    "exportTaskHMDiff2.status()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'state': 'READY',\n",
       " 'description': 'SD2_net_potential_map_Export',\n",
       " 'creation_timestamp_ms': 1675161129989,\n",
       " 'update_timestamp_ms': 1675161129989,\n",
       " 'start_timestamp_ms': 0,\n",
       " 'task_type': 'EXPORT_IMAGE',\n",
       " 'id': 'PQA75UFIEH3U5XF4SYXCSVP3',\n",
       " 'name': 'projects/earthengine-legacy/operations/PQA75UFIEH3U5XF4SYXCSVP3'}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exportTaskSD1 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensitySD1.mask(potentialMask),\n",
    "                                                  description = 'SD1_full_potential_map_Export',\n",
    "                                                  fileNamePrefix = 'ForestBiomassExport/SD1_Full_TGB_potential_Map',\n",
    "                                                  region = unboundedGeo,\n",
    "                                                  bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                  crs = 'EPSG:4326',\n",
    "                                                  crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                  maxPixels = 1e13,\n",
    "                                                  fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskSD1.start()\n",
    "# show the task status\n",
    "exportTaskSD1.status()\n",
    "\n",
    "exportTaskSD2 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensitySD2.mask(potentialMask),\n",
    "                                                  description = 'SD2_full_potential_map_Export',\n",
    "                                                  fileNamePrefix = 'ForestBiomassExport/SD2_Full_TGB_potential_Map',\n",
    "                                                  region = unboundedGeo,\n",
    "                                                  bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                  crs = 'EPSG:4326',\n",
    "                                                  crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                  maxPixels = 1e13,\n",
    "                                                  fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskSD2.start()\n",
    "# show the task status\n",
    "exportTaskSD2.status()\n",
    "\n",
    "\n",
    "\n",
    "exportTaskSDDiff1 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensitySD1.subtract(tgbPresentDensitySD).mask(potentialMask),\n",
    "                                                      description = 'SD1_net_potential_map_Export',\n",
    "                                                      fileNamePrefix = 'ForestBiomassExport/SD1_Net_TGB_potential_Map',\n",
    "                                                      region = unboundedGeo,\n",
    "                                                      bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                      crs = 'EPSG:4326',\n",
    "                                                      crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                      maxPixels = 1e13,\n",
    "                                                      fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskSDDiff1.start()\n",
    "# show the task status\n",
    "exportTaskSDDiff1.status()\n",
    "\n",
    "\n",
    "exportTaskSDDiff2 = ee.batch.Export.image.toCloudStorage(image = tgbPotentialDensitySD2.subtract(tgbPresentDensitySD).mask(potentialMask),\n",
    "                                                      description = 'SD2_net_potential_map_Export',\n",
    "                                                      fileNamePrefix = 'ForestBiomassExport/SD2_Net_TGB_potential_Map',\n",
    "                                                      region = unboundedGeo,\n",
    "                                                      bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                      crs = 'EPSG:4326',\n",
    "                                                      crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                      maxPixels = 1e13,\n",
    "                                                      fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportTaskSDDiff2.start()\n",
    "# show the task status\n",
    "exportTaskSDDiff2.status()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generate the maps for Figure S6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# compose the  images into a multiband image\n",
    "tgbPotentialDensityGS_Comp = tgbPotentialDensityGS_Max1.addBands(tgbPotentialDensityGS_Max2).addBands(tgbPotentialDensityGS_Mean1)\n",
    "tgbPotentialDensityGS = tgbPotentialDensityGS_Comp.reduce(ee.Reducer.mean())\n",
    "\n",
    "# compose the  images into a multiband image\n",
    "tgbPotentialDensitySD_Comp = tgbPotentialDensitySD1.addBands(tgbPotentialDensitySD2).addBands(tgbPotentialDensityHM1).addBands(tgbPotentialDensityHM2).addBands(tgbPotentialDensityWK1).addBands(tgbPotentialDensityWK2)\n",
    "# apply the reducer \n",
    "tgbPotentialDensitySD = tgbPotentialDensitySD_Comp.reduce(ee.Reducer.mean())\n",
    "\n",
    "\n",
    "# get the difference layer of the fuul potential from both GS and SD\n",
    "fullPotentialDensityDiff = tgbPotentialDensityGS.subtract(tgbPotentialDensitySD)\n",
    "\n",
    "# get the present existing biomass for GS and SD\n",
    "tgbPresentDensityGS_Comp = tgbPresentDensityGS_Max.addBands(tgbPresentDensityGS_Mean)\n",
    "# apply the reducer \n",
    "tgbPresentDensityGS = tgbPresentDensityGS_Comp.reduce(ee.Reducer.mean())\n",
    "\n",
    "tgbPresentDensitSD_Comp = tgbPresentDensitySD.addBands(tgbPresentDensityHM).addBands(tgbPresentDensityWK)\n",
    "# apply the reducer \n",
    "tgbPresentDensitySD = tgbPresentDensitSD_Comp.reduce(ee.Reducer.mean())\n",
    "# get the net potential for GS and SD\n",
    "netPotentialDensityGS = tgbPotentialDensityGS.subtract(tgbPresentDensityGS)\n",
    "netPotentialDensitySD = tgbPotentialDensitySD.subtract(tgbPresentDensitySD)\n",
    "# create the net potential difference of this two types for GS and SD\n",
    "netPotentialDensityDiff = netPotentialDensityGS.subtract(netPotentialDensitySD)\n",
    "netPotentialDensityMean = netPotentialDensityGS.add(netPotentialDensitySD).divide(2)\n",
    "# get the ratio of the net difference\n",
    "netPotentialDensityRatio = netPotentialDensityDiff.divide(netPotentialDensityMean)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'state': 'READY',\n",
       " 'description': 'Net_potential_ratio_GS_SD_map_Export',\n",
       " 'creation_timestamp_ms': 1683295104449,\n",
       " 'update_timestamp_ms': 1683295104449,\n",
       " 'start_timestamp_ms': 0,\n",
       " 'task_type': 'EXPORT_IMAGE',\n",
       " 'id': 'PGIUHYGGDOCIWYUJA6OJ5FR6',\n",
       " 'name': 'projects/earthengine-legacy/operations/PGIUHYGGDOCIWYUJA6OJ5FR6'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exportFullDiff01 = ee.batch.Export.image.toCloudStorage(image = fullPotentialDensityDiff.mask(potentialMask),\n",
    "                                                      description = 'Full_potential_diff_GS_SD_map_Export',\n",
    "                                                      fileNamePrefix = 'ForestBiomassExport/Full_TGB_potential_diff_Map_of_GS_SD',\n",
    "                                                      region = unboundedGeo,\n",
    "                                                      bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                      crs = 'EPSG:4326',\n",
    "                                                      crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                      maxPixels = 1e13,\n",
    "                                                      fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportFullDiff01.start()\n",
    "# show the task status\n",
    "exportFullDiff01.status()\n",
    "\n",
    "exportNetDiff01 = ee.batch.Export.image.toCloudStorage(image = netPotentialDensityDiff.mask(potentialMask),\n",
    "                                                      description = 'Net_potential_diff_GS_SD_map_Export',\n",
    "                                                      fileNamePrefix = 'ForestBiomassExport/Net_TGB_potential_diff_Map_of_GS_SD',\n",
    "                                                      region = unboundedGeo,\n",
    "                                                      bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                      crs = 'EPSG:4326',\n",
    "                                                      crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                      maxPixels = 1e13,\n",
    "                                                      fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportNetDiff01.start()\n",
    "# show the task status\n",
    "exportNetDiff01.status()\n",
    "\n",
    "\n",
    "exportNetRatio01 = ee.batch.Export.image.toCloudStorage(image = netPotentialDensityRatio.mask(potentialMask),\n",
    "                                                      description = 'Net_potential_ratio_GS_SD_map_Export',\n",
    "                                                      fileNamePrefix = 'ForestBiomassExport/Net_TGB_potential_ratio_Map_of_GS_SD',\n",
    "                                                      region = unboundedGeo,\n",
    "                                                      bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                      crs = 'EPSG:4326',\n",
    "                                                      crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                      maxPixels = 1e13,\n",
    "                                                      fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "exportNetRatio01.start()\n",
    "# show the task status\n",
    "exportNetRatio01.status()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## check the pixel number difference "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'mean': 33164412}\n",
      "{'mean': 153139268}\n"
     ]
    }
   ],
   "source": [
    "# calculate the pixels with GS is lower than the SD\n",
    "# define the GS is larger map\n",
    "lagerMap = netPotentialDensityDiff.mask(potentialMask).gt(0)\n",
    "largerPixelNumber = lagerMap.reduceRegion(reducer= ee.Reducer.sum(),\n",
    "                                          geometry= unboundedGeo,\n",
    "                                          crs='EPSG:4326',\n",
    "                                          crsTransform=[0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                          maxPixels= 1e13)\n",
    "\n",
    "smallerMap = netPotentialDensityDiff.mask(potentialMask).lt(0)\n",
    "smallerPixelNumber = smallerMap.reduceRegion(reducer= ee.Reducer.sum(),\n",
    "                                             geometry= unboundedGeo,\n",
    "                                             crs='EPSG:4326',\n",
    "                                             crsTransform=[0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                             maxPixels= 1e13)\n",
    "\n",
    "print(largerPixelNumber.getInfo())\n",
    "print(smallerPixelNumber.getInfo())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17.801265117253724"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "33164412/(33164412+153139268)*100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
